package com.c6906.vdam.dcs.model.dto;

import com.c6906.vdam.common.util.StrUtils;
import com.c6906.vdam.dcs.protocol.t808.T0200;

import java.time.format.DateTimeFormatter;

public class TerminalData {
    private String terminalSn;         // 终端唯一编号
    private String terminalSIM;        // 终端SIM卡号
    private String isFixed;            // 终端是否定位，0：未定位，1：已定位
    private String longitude;          // 终端上报经度，以度为单位的纬度值乘以 10 的 6 次方，精确到百万分之一度
    private String latitude;           // 终端上报纬度，以度为单位的纬度值乘以 10 的 6 次方，精确到百万分之一度
    private String elevation;          // 终端上报海拔，米
    private String direction;          // 终端上报方向 1°
    private String terminalTime;       // 终端上报时间,UTC方式，秒
    private String terminalMiles;      // 终端上报里程, 0.1km
    private String speed;              // 终端上报速度，1km/h
    private String terminalOverSpeed;  // 超速警告，0：无超速，1：超速
    private String terminalTiredDrive; // 疲劳驾驶，0：无疲劳驾驶，1：疲劳驾驶
    private String terminalGnssError;  // GNSS模块状态，0：无异常，1：GNSS模块异常
    private String terminalAntOpen;    // GNSS天线开路，0：无异常，1：天线短路
    private String terminalAntShort;   // GNSS天线短路，0：无异常，1：天线开路
    private String terminalUnderVol;   // 终端欠压状态，0：无异常，1：终端欠压
    private String terminalNoVol;      // 终端掉电状态，0：无异常，1：终端掉电
    private String terminalAccStatus;  // 终端ACC状态，0：未激活，1：已激活
    private String terminalNetRssi;    // 无线网络信号强度
    private String terminalGnssCnt;    // GNSS定位卫星数量
    private String fixBeidou;          // 北斗定位状态
    private String fixGps;             // GPS定位状态
    private String fixGlonass;         // GLONASS定位状态
    private String fixGalileo;         // Galileo定位状态

    public TerminalData(T0200 message) {
        this.terminalSn = StrUtils.idTransform(message.getClientId());
        this.terminalSIM = StrUtils.idTransform(message.getClientId());
        this.longitude = String.valueOf(message.getLongitude() / 1000000.0);
        this.latitude = String.valueOf(message.getLatitude() / 1000000.0);
        this.elevation = String.valueOf(message.getAltitude());
        this.direction = String.valueOf(message.getDirection());
        this.terminalTime = message.getDeviceTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        this.speed = String.valueOf(message.getSpeed() / 10.0);
        /* 状态位 */
        this.terminalAccStatus = String.valueOf(message.getStatusBit() & 1);
        this.isFixed = String.valueOf(message.getStatusBit() >> 1 & 1);
        this.fixGps = String.valueOf(message.getStatusBit() >> 18 & 1);
        this.fixBeidou = String.valueOf(message.getStatusBit() >> 19 & 1);
        this.fixGlonass = String.valueOf(message.getStatusBit() >> 20 & 1);
        this.fixGalileo = String.valueOf(message.getStatusBit() >> 21 & 1);
        /* 报警位 */
        this.terminalOverSpeed = String.valueOf(message.getWarnBit() >> 1 & 1);
        this.terminalTiredDrive = String.valueOf(message.getWarnBit() >> 2 & 1);
        this.terminalGnssError = String.valueOf(message.getWarnBit() >> 4 & 1);
        this.terminalAntOpen = String.valueOf(message.getWarnBit() >> 5 & 1);
        this.terminalAntShort = String.valueOf(message.getWarnBit() >> 6 & 1);
        this.terminalUnderVol = String.valueOf(message.getWarnBit() >> 7 & 1);
        this.terminalNoVol = String.valueOf(message.getWarnBit() >> 8 & 1);
        /* 位置附加信息 */
        if (message.getAttributes() != null) {
            if (message.getAttributes().containsKey(0x01))
                this.terminalMiles = String.valueOf((((Number) message.getAttributes().get(0x01)).intValue() / 10.0));
            if (message.getAttributes().containsKey(0x30))
                this.terminalNetRssi = message.getAttributes().get(0x30).toString();
            if (message.getAttributes().containsKey(0x31))
                this.terminalGnssCnt = message.getAttributes().get(0x31).toString();
        }
    }

    // Getters and Setters
    public String getTerminalSn() {
        return terminalSn;
    }

    public void setTerminalSn(String terminalSn) {
        this.terminalSn = terminalSn;
    }

    public String getTerminalSIM() {
        return terminalSIM;
    }

    public void setTerminalSIM(String terminalSIM) {
        this.terminalSIM = terminalSIM;
    }

    public String getIsFixed() {
        return isFixed;
    }

    public void setIsFixed(String isFixed) {
        this.isFixed = isFixed;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getElevation() {
        return elevation;
    }

    public void setElevation(String elevation) {
        this.elevation = elevation;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public String getTerminalTime() {
        return terminalTime;
    }

    public void setTerminalTime(String terminalTime) {
        this.terminalTime = terminalTime;
    }

    public String getTerminalMiles() {
        return terminalMiles;
    }

    public void setTerminalMiles(String terminalMiles) {
        this.terminalMiles = terminalMiles;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getTerminalOverSpeed() {
        return terminalOverSpeed;
    }

    public void setTerminalOverSpeed(String terminalOverSpeed) {
        this.terminalOverSpeed = terminalOverSpeed;
    }

    public String getTerminalTiredDrive() {
        return terminalTiredDrive;
    }

    public void setTerminalTiredDrive(String terminalTiredDrive) {
        this.terminalTiredDrive = terminalTiredDrive;
    }

    public String getTerminalGnssError() {
        return terminalGnssError;
    }

    public void setTerminalGnssError(String terminalGnssError) {
        this.terminalGnssError = terminalGnssError;
    }

    public String getTerminalAntOpen() {
        return terminalAntOpen;
    }

    public void setTerminalAntOpen(String terminalAntOpen) {
        this.terminalAntOpen = terminalAntOpen;
    }

    public String getTerminalAntShort() {
        return terminalAntShort;
    }

    public void setTerminalAntShort(String terminalAntShort) {
        this.terminalAntShort = terminalAntShort;
    }

    public String getTerminalUnderVol() {
        return terminalUnderVol;
    }

    public void setTerminalUnderVol(String terminalUnderVol) {
        this.terminalUnderVol = terminalUnderVol;
    }

    public String getTerminalNoVol() {
        return terminalNoVol;
    }

    public void setTerminalNoVol(String terminalNoVol) {
        this.terminalNoVol = terminalNoVol;
    }

    public String getTerminalAccStatus() {
        return terminalAccStatus;
    }

    public void setTerminalAccStatus(String terminalAccStatus) {
        this.terminalAccStatus = terminalAccStatus;
    }

    public String getTerminalNetRssi() {
        return terminalNetRssi;
    }

    public void setTerminalNetRssi(String terminalNetRssi) {
        this.terminalNetRssi = terminalNetRssi;
    }

    public String getTerminalGnssCnt() {
        return terminalGnssCnt;
    }

    public void setTerminalGnssCnt(String terminalGnssCnt) {
        this.terminalGnssCnt = terminalGnssCnt;
    }

    public String getFixBeidou() {
        return fixBeidou;
    }

    public void setFixBeidou(String fixBeidou) {
        this.fixBeidou = fixBeidou;
    }

    public String getFixGps() {
        return fixGps;
    }

    public void setFixGps(String fixGps) {
        this.fixGps = fixGps;
    }

    public String getFixGlonass() {
        return fixGlonass;
    }

    public void setFixGlonass(String fixGlonass) {
        this.fixGlonass = fixGlonass;
    }

    public String getFixGalileo() {
        return fixGalileo;
    }

    public void setFixGalileo(String fixGalileo) {
        this.fixGalileo = fixGalileo;
    }
}
